package cn.lxs.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String checkCode = req.getParameter("checkCode");
        HttpSession session  = req.getSession();
        //先获取生成的验证码
       String checkCode_see = (String)  session.getAttribute("checkCode_see");
       session.removeAttribute("checkCode_see");
       //获取完验证码反手删除,以达到一次性验证码的作用
        if(checkCode_see!=null&&checkCode_see.equalsIgnoreCase(checkCode))
        {
            //这是一个不区分大小写的比较方法
            if("li".equals(username)&&"123".equals(password))
            {
                //登录成功

                session.setAttribute("user",username);
                //重定向到success.jsp
                resp.sendRedirect(req.getContextPath()+"/success.jsp");

            }
            else
            {
                req.setAttribute("login_error","用户名或密码错误");
                req.getRequestDispatcher("/Login.jsp").forward(req,resp);
            }
        }
        else
        {
            req.setAttribute("cc_error","验证码错误");
            req.getRequestDispatcher("/Login.jsp").forward(req,resp);
        }
    }
}
